#ifndef __DSTH_CLIENT_BOOT_STRAP_H__
#define __DSTH_CLIENT_BOOT_STRAP_H__

#include <string>
#include <memory>
#include "EventLoop.h"
#include "Connection.h"

namespace dsth {
class Connector;
class ClientBootstrap {
public:
    typedef Delegate<void ()> ConnectRefused;
    ClientBootstrap(EventLoop* loop, const InetSocketAddress& server_addr, const char* name);
    ~ClientBootstrap();
    void Connect();
    void Disconnect();

    void SetOnConnection(Connection::ConnectionHandler handler) { conn_handler_ = handler; }
    void SetOnDisConnection(Connection::ConnectionHandler handler) { dis_conn_handler_ = handler; }
    void SetOnMessage(Connection::MessageHandler handler) { msg_handler_ = handler; }
    void SetOnRefused(ConnectRefused handler) { refused_handler_ = handler; }
    const char* GetName() const { return name_.c_str(); }
    Connection* GetConnection() { return conn_ptr_.get(); }

private:
    // Can't invoke copy constructor or assignment operator
    ClientBootstrap(const ClientBootstrap&);
    void operator=(const ClientBootstrap&);

    void OnNewConnection(int fd);
    void OnCloseConnection(Connection* conn);
    void OnRefused();

    EventLoop* loop_;
    std::auto_ptr<Connector> connector_;
    const std::string name_;
    std::auto_ptr<Connection> conn_ptr_;

    Connection::ConnectionHandler conn_handler_;
    Connection::ConnectionHandler dis_conn_handler_;
    Connection::MessageHandler msg_handler_;
    ConnectRefused refused_handler_;
};

} // namespace
#endif // __DSTH_CLIENT_BOOT_STRAP_H__
